<?php
namespace Admin\Controller;

class DlycorpController extends BaseController
{


    public function dlyCorp()
    {

        $wl = M('b2c_dlycorp')->select();


         $seDly = M('b2c_dlycorp d')
                      ->join(C('DB_PREFIX').'b2c_store_dlycorp sd on d.corp_id = sd.corp_id', 'LEFT')
                      ->where(['local_id'=>session('userInfo')['local_id']])
                      ->select();

        foreach ($wl as $k=>$value){
            foreach ($seDly as $j=>$item){
                if($item['corp_id']==$value['corp_id']){
                    $wl[$k]['isSelect'] = 1;
                }
            }
        }

        $this->assign('wlist',$wl);
        $this->display('Dlycorp/dlyCorp');
    }
    public function save(){

        $data = $_POST['data'];


        foreach ($data as $k=>$value){
            $dataAdd[$k]['corp_id'] = $value[0];
            $dataAdd[$k]['name'] = $value[1];
            $dataAdd[$k]['local_id'] = session('userInfo')['local_id'];

            $isExit =M('b2c_store_dlycorp')->where(['corp_id'=>$value[0],'name'=>$value[1],'local_id'=>session('userInfo')['local_id']])->find();

            if(empty($isExit)){
               M('b2c_store_dlycorp')->add($dataAdd[$k]);
            }


        }

        $seDly = M('b2c_store_dlycorp')->where(['local_id'=>session('userInfo')['local_id']])->select();
        foreach ($seDly as $k=>$value){
          $isin = $this->deep_in_array($value['corp_id'], $dataAdd);
            if($isin === false){
                M('b2c_store_dlycorp')->where(['corp_id'=>$value['corp_id'],'local_id'=>session('userInfo')['local_id']])->delete();
            }
        }


        $this->ajaxReturn(array('msg'=>'物流设置成功！','status'=>1));

    }

    public function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }
            if(in_array($value, $item)) {
                return true;
            } else if($this->deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }

}